package com.jy.novel.controller.base;

import com.jy.novel.pojo.system.UserInfo;
import com.jy.novel.common.CommonResult;
import com.jy.novel.common.Fields;

import javax.servlet.http.HttpSession;

/**
 * Created by jihb on 4/12/16.
 */
public class BaseController {

    public UserInfo getUserInfo(HttpSession session){
        Object object = session.getAttribute(Fields.LOGIN_USER_INFO);
        if (object != null && object instanceof UserInfo){
            return (UserInfo)object;
        }
        return null;
    }

    public CommonResult returnWrap(String message, int code, Object data){
        return  new CommonResult(code,message,data,code==0);
    }

    public CommonResult returnDataWrap(Object data){
        return returnWrap("success",0,data);
    }
    public CommonResult returnBoolWrap(boolean success){
        return returnBoolWrap(success,success?"success":"error");
    }
    public CommonResult returnBoolWrap(boolean success, String message){
        return returnWrap(success?"success":message,success?0:-1,null);
    }
    public CommonResult returnFailure(String message){
        return returnFailure(message,null);
    }
    public CommonResult returnFailure(String message, Object data){
        return returnWrap(message,-1,data);
    }
}
